package thread;

import java.util.*;

public class SyApiDemo2 {
    public static void main(String[] args) {
        //Collection<Integer> c = new ArrayList<>();
        Collection<Integer> c = Collections.synchronizedCollection(new ArrayList<>());
        Thread t1=new Thread()
        {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    c.add(i);
                }
            }
        };
        Thread t2=new Thread()
        {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    c.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(c.size());
    }
}
